from flask import Blueprint,jsonify,request
from db import db1
import json

bp_cbf = Blueprint('cflask',__name__)


@bp_cbf.route('/yw',methods=['POST','GET'])
def yw():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = """insert into yw values (%d,'%s','%s')"""%(0,data['name'],'')
        db1.insert(sql)
        return jsonify({'code':200})
    elif request.method == 'GET':
        sql = """select * from yw"""
        list = db1.find_all(sql)
        return jsonify({'code':200,"list":list})

@bp_cbf.route('/setparams',methods=['POST','GET'])
def setparams():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = "update yw set canshu='%s' where id=%d" % (data['canshu'], int(data['id']))
        db1.insert(sql)
        return jsonify({"code": 200})
    elif request.method=='GET':
        wid = request.args.get('wid')
        sql = """select * from yw where id=%d"""%(int(wid))
        res = db1.find_one(sql)
        return jsonify({'code': 200, "list": res})

@bp_cbf.route('/yhlM',methods=['POST','GET'])
def yhlM():
    if request.method == 'POST':
        data = request.data
        if data:
            data = json.loads(data)
        else:
            data = request.form
        sql = """insert into yhl values (%d,'%d','%d','%s')""" % (0,int(data['uid']),int(data['wid']),data['pamars'])
        db1.insert(sql)
        return jsonify({'code': 200})